package com.tangding.shiro.configurations;

import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.tangding.common.data.Constants;

/**
 * @description session管理配置
 *
 * @author Ranger
 * @version 2017年11月8日_下午7:12:43
 *
 */
@Configuration
public class ShiroSessionConfiguration {
	
	@Bean(name="sessionManager")
    public SessionManager sessionManager(
    		@Qualifier("redisSessionDao") AbstractSessionDAO sessionDao, 
    		@Qualifier("sessionIdCookie") Cookie sessionIdCookie,
    		@Qualifier("sessionIdGenerator") JavaUuidSessionIdGenerator sessionIdGenerator) {
//		sessionDao.setActiveSessionsCacheName("shiro-activeSessionCache");
		sessionDao.setSessionIdGenerator(sessionIdGenerator);
    	DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    	sessionManager.setSessionDAO(sessionDao);
    	sessionManager.setSessionIdCookie(sessionIdCookie);
    	sessionManager.setSessionValidationSchedulerEnabled(true);
    	sessionManager.setGlobalSessionTimeout(Constants.LOGIN_SESSION_EXPIRED);
    	return sessionManager;
    }
	
	@Bean(name="sessionIdGenerator")
	public JavaUuidSessionIdGenerator sessionIdGenerator() {
		return new JavaUuidSessionIdGenerator();
	}
	
	@Bean(name="sessionIdCookie")
	public Cookie sessionIdCookie() {
		Cookie sc = new SimpleCookie("sessionId");
		sc.setMaxAge(Constants.LOGIN_SESSION_EXPIRED);
    	sc.setPath("/");
    	sc.setHttpOnly(true);
    	return sc;
	}
}
